package cn.edu.scau.cmi.houzhicong.combinationPattern.consistentPattern;

import java.util.List;
import java.util.ArrayList;

public class ConsistentOrganization extends ConsistentComponent {
	
	
	private List<ConsistentComponent> children = new ArrayList<>();
	
	public ConsistentOrganization(String name) {
		super(name);
	}


	public List<ConsistentComponent> getChildren() {
		return children;
	}

	public boolean  addChild(ConsistentComponent child){
		child.setParent(this);
		boolean isSuccess = children.add(child);
		if(isSuccess){
			child.setParent(this);
		}
		return isSuccess;
	}


	public boolean  removeChild(ConsistentComponent child){
		return children.remove(child );
	}


	public String showChildren(){
		StringBuilder sb = new StringBuilder();
		for (ConsistentComponent child :
				children) {
			sb.append(child.getName()).append(", ");
		}
		String retString = sb.toString();
		if(retString.length()>=2){
			return retString.substring(0,retString.length()-2);
		}
		return retString;
	}

	@Override
	public String display() {
		return String.format("This is an organization named %s and it has %d %s", name,
				children.size(),children.size()>1?"children":"child");
	}
	
}
